package com.example.hotelmanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hotelmanager.entity.Cost;
import com.example.hotelmanager.mapper.CostMapper;
import com.example.hotelmanager.service.CostService;
import com.example.hotelmanager.service.CostTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author :chenjie
 * @date :Created 2022/11/27 22:51
 */
@Service
public class CostServiceImpl extends ServiceImpl<CostMapper, Cost>implements CostService {
    @Autowired
    private CostTypeService costTypeService;
    @Override
    public void settleCostByRoomId(String roomId) {
        this.baseMapper.settleCostByRoomId(roomId);
    }

    @Override
    public Map<String, Object> getCostByRoomId(String roomId) {
        LambdaQueryWrapper<Cost> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Cost::getRoomId,roomId);
        List<Cost> costs = this.baseMapper.selectList(queryWrapper);
        List<Cost> costs1 = new ArrayList<>();
        if(costs != null && costs.size()>0) {
            double hasSettle = 0;
            double toll = 0;
            for (Cost cost : costs) {
                if (cost.getState() != 11) {
                    toll += costTypeService.getById(cost.getCostTypeId()).getMoney() * cost.getNum();
                    if (cost.getState() == 1) {
                        hasSettle += costTypeService.getById(cost.getCostTypeId()).getMoney() * cost.getNum();
                    }
                    costs1.add(cost);
                }
            }
            double needSettle = toll - hasSettle;
            Map<String, Object> resultMap = new HashMap<>();

            resultMap.put("costs1", costs1);
            resultMap.put("toll", toll);
            resultMap.put("hasSettle", hasSettle);
            resultMap.put("needSettle", needSettle);
            return resultMap;
        }
        return null;
    }
}
